package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.PointCategory;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminPointCatBean extends AbstractBean {

	public static final String BEAN_NAME="adminPointCatBean";
	public static final String NAV_LIST="adminPointCatList";
	public static final String NAV_CREATE="adminPointCatCreate";
	public static final String NAV_EDIT="adminPointCatEdit";
	
	private MembershipService service;
	private PointCategory category;
	private List<PointCategory> categoryList;
	public PointCategory getCategory() {
		return category;
	}
	public void setCategory(PointCategory category) {
		this.category = category;
	}

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}
	
	public List<PointCategory> getCategoryList() {
		try{
			categoryList = service.getPointCategoryList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return categoryList;
	}
	
	public String navCreateAction(){
		category = new PointCategory();
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			service.createPointCategory(category, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("pointId");
		String id = (String)param.getValue();
		for(PointCategory cat:categoryList){
			if(cat.getPointCatId().equalsIgnoreCase(id)){
				category = cat;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			service.editPointCategory(category, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deletePointCategory(categoryList, getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}
}
